<?php

class PatientController extends Zend_Controller_Action {

    //protected $_container;

    public function init() {
        /* Initialize action controller here */
       // $this->view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
    }

    public function dateAction() {

        $form = new ZendX_JQuery_Form();
        $date = new ZendX_JQuery_Form_Element_DatePicker('date', array('label' => 'Date'));
        $date->setJQueryParam('dateFormat', 'dd-mm-yy');
        $form->addElement($date);
        $this->view->form = $form;
    }

    public function indexAction() {

        // AutoComplete
        $source = $this->view->url(array('controller' => 'patient', 'action' => 'autocomplete'));
        $form = new Application_Form_SubForm_AutoComplete(array('source' => $source));
        $this->view->form = $form;

        // Paginator
        $patient = new Application_Model_DbTable_Patient();
        $paginator = Zend_Paginator::factory($patient->getAllPatient());
        $paginator->setItemCountPerPage(6);
        $paginator->setCurrentPageNumber($this->getRequest()->getParam('page'));
        $this->view->patient = $paginator;
    }

    public function autocompleteAction() {

        $view = $this->view;
        $term = $this->_getParam('term');
        $patient = new Application_Model_DbTable_Patient();
        $results = $patient->getPatientByName($term);

        // url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
        foreach ($results as $value) {

            $values[] = array(
                'href' => $view->url(array(
                    'controller' => 'patient',
                    'action' => 'edit',
                    'id' => $value['id_patient']), null, true),
                'label' => $value['nom'] . " " . $value['prenom']);
        }
        $this->_helper->json(array_values($values));
    }

    public function addAction() {

        $form = new Application_Form_Patient('Ajouter', 'Nouveau');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            if ($form->isValid($data)) {
                $patient = new Application_Model_DbTable_Patient();
                $patient->addPatient($form->getValidValues($data));
                $this->_helper->redirector('index');
            } else {
                $form->populate($data);
            }
        }
    }

    public function editAction() {

        $form = new Application_Form_Patient('Enregistrer', 'Edition');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            if ($form->isValid($data)) {
                //Zend_Debug::dump($form->getValidValues($data));
                $patient = new Application_Model_DbTable_Patient();
                $patient->editPatient($form->getValidValues($data));
                $this->_helper->redirector('index');
            } else {
                $form->populate($data);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $patient = new Application_Model_DbTable_Patient();
                // Zend_Debug::dump($patient->getPatient($id));
                $form->populate($patient->getPatient($id));
            }
        }
    }

    public function deleteAction() {

        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if (strtolower($del) == 'oui') {
                $id = $this->getRequest()->getPost('id');
                $patient = new Application_Model_DbTable_Patient();
                $patient->deletePatient($id);
            }
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);

            if ($id > 0) {
                $patient = new Application_Model_DbTable_Patient();
                $this->view->patient = $patient->getPatient($id);
            }
        }
    }

}
